package com.ivt.controller.teacher;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.ivt.core.util.Constants;
import com.ivt.model.Classes;
import com.ivt.model.Student;
import com.ivt.model.view.QueryCriteria;
import com.ivt.model.view.Rst;
import com.ivt.model.view.StudentView;
import com.ivt.service.ClassesService;
import com.ivt.service.StudentService;
import com.ivt.utils.Convert;
import com.ivt.utils.enums.AuditStatus;

@Controller
@RequestMapping("/teacher/student")
public class StudentController {

	private final static Logger LOGGER = LoggerFactory
			.getLogger(StudentController.class);

	@Resource
	private StudentService studentService;

	@Resource
	private ClassesService classesService;

	@ModelAttribute
	public void attr(ModelMap view) {
		LOGGER.debug("student");
		view.addAttribute("moduleName", "student");
	}

	@RequestMapping("/create")
	public String create(ModelMap view) {
		view.addAttribute("moduleAction", "save");
		List<Classes> classes = classesService
				.queryClassesByStatus(AuditStatus.PASS);
		view.addAttribute("classes", classes);
		return "alone:teacher.student-create";
	}

	@RequestMapping("/list")
	public String list(ModelMap view) {
		return "alone:teacher.student-list";
	}

	@RequestMapping("/modify/{id}")
	public String modify(@PathVariable Long id, ModelMap view) {

		Student st = studentService.getStudent(id);

		view.addAttribute("row", st);
		view.addAttribute("moduleAction", "update");
		List<Classes> classes = classesService
				.queryClassesByStatus(AuditStatus.PASS);
		view.addAttribute("classes", classes);

		return "alone:teacher.student-modify";
	}

	@RequestMapping("/save")
	@ResponseBody
	public Rst save(Student student) {

		Rst rst = new Rst();
		student = studentService.create(student);
		if (0 < student.getId()) {
			rst.setCode(Constants.SUCCESS);
			rst.setMessage("操作成功");
		}
		return rst;
	}

	@RequestMapping("/pagination")
	@ResponseBody
	public Rst pagination(QueryCriteria queryCriteria) {

		Rst rst = new Rst();
		List<Student> list = studentService
				.queryClassesByCriteria(queryCriteria);

		List<StudentView> views = Lists.newArrayList();
		if (CollectionUtils.isNotEmpty(list)) {
			for (Student student : list) {
				views.add(Convert.toStudentView(student));
			}
		}
		rst.setCode(Constants.SUCCESS);
		rst.setData(views);
		return rst;
	}

	@RequestMapping("/pages")
	@ResponseBody
	public Rst pages(QueryCriteria queryCriteria) {

		Rst rst = new Rst();
		long count = studentService.queryCount(queryCriteria);
		rst.setCode(Constants.SUCCESS);
		rst.setData(count);
		return rst;
	}

	@RequestMapping("/update")
	@ResponseBody
	public Rst update(Student student) {
		Rst rst = new Rst();
		student = studentService.updateStudent(student);
		if (0 < student.getId()) {
			rst.setCode(Constants.SUCCESS);
			rst.setMessage("操作成功");
		}
		return rst;
	}

	@RequestMapping("/del")
	@ResponseBody
	public Rst update(Long[] ids) {
		Rst rst = new Rst();
		int c = studentService.deletes(ids);
		rst.setCode(Constants.SUCCESS);
		rst.setMessage("操作成功");
		rst.setData(c);
		return rst;
	}
}
